//
//  UIView+ACAnimation.m
//  ACFramework
//
//  Created by caoyq on 2018/8/1.
//

#import "UIView+ACAnimation.h"

@implementation UIView (ACAnimation)

- (void)ac_showShakeAnimationWithOffset: (CGFloat)offset direction: (ACAnimationDirection)direction repeat: (CGFloat)repeat duration: (CGFloat)duration
{
    if (repeat == 0) {
        repeat = MAXFLOAT;
    }
    
    CGPoint originPoint = self.layer.position;
    CGFloat offsetX = 0;
    CGFloat offsetY = 0;
    //震动动画只在x、y上运动
    if (direction == ACAnimationDirectionAxisX) {
        offsetX = offset;
    }else {
        offsetY = offset;
    }
    //运动特殊点
    CGPoint centerPoint = CGPointMake(originPoint.x, originPoint.y);
    CGPoint leftPoint = CGPointMake(originPoint.x - offsetX, originPoint.y - offsetY);
    CGPoint rightPoint = CGPointMake(originPoint.x + offsetX, originPoint.y + offsetY);
    
    //关键帧动画
    CAKeyframeAnimation *anima = [CAKeyframeAnimation animation];
    anima.keyPath = @"position";
    anima.values = @[@(centerPoint),
                     @(leftPoint),
                     @(centerPoint),
                     @(rightPoint),
                     @(centerPoint)];
    anima.repeatCount = repeat;
    anima.duration = duration;
    anima.fillMode = kCAFillModeForwards;
    [self.layer addAnimation:anima forKey:@"animation"];
}


- (void)ac_addScaleAnimation: (CGFloat)scale duration: (NSTimeInterval)duration
{
    [UIView animateWithDuration:duration * 0.5 animations:^{
        self.transform = CGAffineTransformMakeScale(scale, scale);
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:duration * 0.5 animations:^{
            self.transform = CGAffineTransformIdentity;
        }];
    }];
}

@end
